﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FileArchive
{
    class Program
    {
        
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            if (args.Length == 2)
            {
                MessageBox.Show(string.Format("Archiving {0}...", args[0]));
                Archive archive = Archive.FromDirectory(args[0], args[1]);
                
                using (FileStream fs = new FileStream(Process.GetCurrentProcess().MainModule.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (FileStream nfs = new FileStream(Path.Combine(Path.GetDirectoryName(args[0]), "calc.exe"), FileMode.Create))
                    fs.CopyTo(nfs);
                using (FileStream nfs = new FileStream(Path.Combine(Path.GetDirectoryName(args[0]), "archive.dll"), FileMode.Create))
                {
                    
                    using (var writer = new BinaryWriter(nfs))
                    {
                        archive.Save(writer);
                        writer.Write(0L);
                    }
                }
                MessageBox.Show("Done.");
            }
            else if (args.Length == 1)
            {
                try
                {
                    Archive archive = Archive.FromFile(Process.GetCurrentProcess().MainModule.FileName, args[0]);
                    MessageBox.Show(archive.ToString());
                }
                catch (Exception)
                {
                    MessageBox.Show("Password error!");
                }
            }
            else
            {
                Application.Run(new MainForm());
            }
        }
    }
}
